package com.hk.common.shiro.token;


import com.hk.common.redis.Redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author zhuiyv
 * @date 2019-12-10
 */
@Order(1)
public class LoginFilter implements Filter {
	
    @Autowired
    private Redis redis;

    @Autowired
	private JwtTokenUtil jwtTokenUtil;
	
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
        
        String token = request.getHeader("Authorization");
		String userName = jwtTokenUtil.getUserName(token);

		token = jwtTokenUtil.verify(token,userName);
		if(token!=null){
			response.setHeader("Authorization",token);
		}
        chain.doFilter(request,response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void destroy() {
	}

}
